<!doctype html>

<html>
<head>
  <link rel="shortcut icon" href="static/images/favicon.ico" type="image/x-icon">
  <title>loosemock.js (Closure Library API Documentation - JavaScript)</title>
  <link rel="stylesheet" href="static/css/base.css">
  <link rel="stylesheet" href="static/css/doc.css">
  <link rel="stylesheet" href="static/css/sidetree.css">
  <link rel="stylesheet" href="static/css/prettify.css">

  <script>
     var _staticFilePath = "static/";
     var _typeTreeName = "goog";
     var _fileTreeName = "Source";
  </script>

  <script src="static/js/doc.js">
  </script>


  <meta charset="utf8">
</head>

<body onload="grokdoc.onLoad();">

<div id="header">
  <div class="g-section g-tpl-50-50 g-split">
    <div class="g-unit g-first">
      <a id="logo" href="index.html">Closure Library API Documentation</a>
    </div>

    <div class="g-unit">
      <div class="g-c">
        <strong>Go to class or file:</strong>
        <input type="text" id="ac">
      </div>
    </div>
  </div>
</div>

<div class="clear"></div>

<h2><a href="closure_goog_testing_loosemock.js.html">loosemock.js</a></h2>

<pre class="prettyprint lang-js">
<a name="line1"></a>// Copyright 2008 The Closure Library Authors. All Rights Reserved.
<a name="line2"></a>//
<a name="line3"></a>// Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
<a name="line4"></a>// you may not use this file except in compliance with the License.
<a name="line5"></a>// You may obtain a copy of the License at
<a name="line6"></a>//
<a name="line7"></a>//      http://www.apache.org/licenses/LICENSE-2.0
<a name="line8"></a>//
<a name="line9"></a>// Unless required by applicable law or agreed to in writing, software
<a name="line10"></a>// distributed under the License is distributed on an &quot;AS-IS&quot; BASIS,
<a name="line11"></a>// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
<a name="line12"></a>// See the License for the specific language governing permissions and
<a name="line13"></a>// limitations under the License.
<a name="line14"></a>
<a name="line15"></a>/**
<a name="line16"></a> * @fileoverview This file defines a loose mock implementation.
<a name="line17"></a> */
<a name="line18"></a>
<a name="line19"></a>goog.provide(&#39;goog.testing.LooseExpectationCollection&#39;);
<a name="line20"></a>goog.provide(&#39;goog.testing.LooseMock&#39;);
<a name="line21"></a>
<a name="line22"></a>goog.require(&#39;goog.array&#39;);
<a name="line23"></a>goog.require(&#39;goog.structs.Map&#39;);
<a name="line24"></a>goog.require(&#39;goog.testing.Mock&#39;);
<a name="line25"></a>
<a name="line26"></a>
<a name="line27"></a>
<a name="line28"></a>/**
<a name="line29"></a> * This class is an ordered collection of expectations for one method. Since
<a name="line30"></a> * the loose mock does most of its verification at the time of $verify, this
<a name="line31"></a> * class is necessary to manage the return/throw behavior when the mock is
<a name="line32"></a> * being called.
<a name="line33"></a> * @constructor
<a name="line34"></a> */
<a name="line35"></a>goog.testing.LooseExpectationCollection = function() {
<a name="line36"></a>  /**
<a name="line37"></a>   * The list of expectations. All of these should have the same name.
<a name="line38"></a>   * @type {Array.&lt;goog.testing.MockExpectation&gt;}
<a name="line39"></a>   * @private
<a name="line40"></a>   */
<a name="line41"></a>  this.expectations_ = [];
<a name="line42"></a>};
<a name="line43"></a>
<a name="line44"></a>
<a name="line45"></a>/**
<a name="line46"></a> * Adds an expectation to this collection.
<a name="line47"></a> * @param {goog.testing.MockExpectation} expectation The expectation to add.
<a name="line48"></a> */
<a name="line49"></a>goog.testing.LooseExpectationCollection.prototype.addExpectation =
<a name="line50"></a>    function(expectation) {
<a name="line51"></a>  this.expectations_.push(expectation);
<a name="line52"></a>};
<a name="line53"></a>
<a name="line54"></a>
<a name="line55"></a>/**
<a name="line56"></a> * Gets the list of expectations in this collection.
<a name="line57"></a> * @return {Array.&lt;goog.testing.MockExpectation&gt;} The array of expectations.
<a name="line58"></a> */
<a name="line59"></a>goog.testing.LooseExpectationCollection.prototype.getExpectations = function() {
<a name="line60"></a>  return this.expectations_;
<a name="line61"></a>};
<a name="line62"></a>
<a name="line63"></a>
<a name="line64"></a>
<a name="line65"></a>/**
<a name="line66"></a> * This is a mock that does not care about the order of method calls. As a
<a name="line67"></a> * result, it won&#39;t throw exceptions until verify() is called. The only
<a name="line68"></a> * exception is that if a method is called that has no expectations, then an
<a name="line69"></a> * exception will be thrown.
<a name="line70"></a> * @param {Object} objectToMock The object to mock.
<a name="line71"></a> * @param {boolean=} opt_ignoreUnexpectedCalls Whether to ignore unexpected
<a name="line72"></a> *     calls.
<a name="line73"></a> * @param {boolean=} opt_mockStaticMethods An optional argument denoting that
<a name="line74"></a> *     a mock should be constructed from the static functions of a class.
<a name="line75"></a> * @param {boolean=} opt_createProxy An optional argument denoting that
<a name="line76"></a> *     a proxy for the target mock should be created.
<a name="line77"></a> * @constructor
<a name="line78"></a> * @extends {goog.testing.Mock}
<a name="line79"></a> */
<a name="line80"></a>goog.testing.LooseMock = function(objectToMock, opt_ignoreUnexpectedCalls,
<a name="line81"></a>    opt_mockStaticMethods, opt_createProxy) {
<a name="line82"></a>  goog.testing.Mock.call(this, objectToMock, opt_mockStaticMethods,
<a name="line83"></a>      opt_createProxy);
<a name="line84"></a>
<a name="line85"></a>  /**
<a name="line86"></a>   * A map of method names to a LooseExpectationCollection for that method.
<a name="line87"></a>   * @type {goog.structs.Map}
<a name="line88"></a>   * @private
<a name="line89"></a>   */
<a name="line90"></a>  this.$expectations_ = new goog.structs.Map();
<a name="line91"></a>
<a name="line92"></a>  /**
<a name="line93"></a>   * The calls that have been made; we cache them to verify at the end. Each
<a name="line94"></a>   * element is an array where the first element is the name, and the second
<a name="line95"></a>   * element is the arguments.
<a name="line96"></a>   * @type {Array.&lt;Array.&lt;*&gt;&gt;}
<a name="line97"></a>   * @private
<a name="line98"></a>   */
<a name="line99"></a>  this.$calls_ = [];
<a name="line100"></a>
<a name="line101"></a>  /**
<a name="line102"></a>   * Whether to ignore unexpected calls.
<a name="line103"></a>   * @type {boolean}
<a name="line104"></a>   * @private
<a name="line105"></a>   */
<a name="line106"></a>  this.$ignoreUnexpectedCalls_ = !!opt_ignoreUnexpectedCalls;
<a name="line107"></a>};
<a name="line108"></a>goog.inherits(goog.testing.LooseMock, goog.testing.Mock);
<a name="line109"></a>
<a name="line110"></a>
<a name="line111"></a>/**
<a name="line112"></a> * A setter for the ignoreUnexpectedCalls field.
<a name="line113"></a> * @param {boolean} ignoreUnexpectedCalls Whether to ignore unexpected calls.
<a name="line114"></a> * @return {goog.testing.LooseMock} This mock object.
<a name="line115"></a> */
<a name="line116"></a>goog.testing.LooseMock.prototype.$setIgnoreUnexpectedCalls = function(
<a name="line117"></a>    ignoreUnexpectedCalls) {
<a name="line118"></a>  this.$ignoreUnexpectedCalls_ = ignoreUnexpectedCalls;
<a name="line119"></a>  return this;
<a name="line120"></a>};
<a name="line121"></a>
<a name="line122"></a>
<a name="line123"></a>/** @override */
<a name="line124"></a>goog.testing.LooseMock.prototype.$recordExpectation = function() {
<a name="line125"></a>  if (!this.$expectations_.containsKey(this.$pendingExpectation.name)) {
<a name="line126"></a>    this.$expectations_.set(this.$pendingExpectation.name,
<a name="line127"></a>        new goog.testing.LooseExpectationCollection());
<a name="line128"></a>  }
<a name="line129"></a>
<a name="line130"></a>  var collection = this.$expectations_.get(this.$pendingExpectation.name);
<a name="line131"></a>  collection.addExpectation(this.$pendingExpectation);
<a name="line132"></a>};
<a name="line133"></a>
<a name="line134"></a>
<a name="line135"></a>/** @override */
<a name="line136"></a>goog.testing.LooseMock.prototype.$recordCall = function(name, args) {
<a name="line137"></a>  if (!this.$expectations_.containsKey(name)) {
<a name="line138"></a>    if (this.$ignoreUnexpectedCalls_) {
<a name="line139"></a>      return;
<a name="line140"></a>    }
<a name="line141"></a>    this.$throwCallException(name, args);
<a name="line142"></a>  }
<a name="line143"></a>
<a name="line144"></a>  // Start from the beginning of the expectations for this name,
<a name="line145"></a>  // and iterate over them until we find an expectation that matches
<a name="line146"></a>  // and also has calls remaining.
<a name="line147"></a>  var collection = this.$expectations_.get(name);
<a name="line148"></a>  var matchingExpectation = null;
<a name="line149"></a>  var expectations = collection.getExpectations();
<a name="line150"></a>  for (var i = 0; i &lt; expectations.length; i++) {
<a name="line151"></a>    var expectation = expectations[i];
<a name="line152"></a>    if (this.$verifyCall(expectation, name, args)) {
<a name="line153"></a>      matchingExpectation = expectation;
<a name="line154"></a>      if (expectation.actualCalls &lt; expectation.maxCalls) {
<a name="line155"></a>        break;
<a name="line156"></a>      } // else continue and see if we can find something that does match
<a name="line157"></a>    }
<a name="line158"></a>  }
<a name="line159"></a>  if (matchingExpectation == null) {
<a name="line160"></a>    this.$throwCallException(name, args, expectation);
<a name="line161"></a>  }
<a name="line162"></a>
<a name="line163"></a>  matchingExpectation.actualCalls++;
<a name="line164"></a>  if (matchingExpectation.actualCalls &gt; matchingExpectation.maxCalls) {
<a name="line165"></a>    this.$throwException(&#39;Too many calls to &#39; + matchingExpectation.name +
<a name="line166"></a>            &#39;\nExpected: &#39; + matchingExpectation.maxCalls + &#39; but was: &#39; +
<a name="line167"></a>            matchingExpectation.actualCalls);
<a name="line168"></a>  }
<a name="line169"></a>
<a name="line170"></a>  this.$calls_.push([name, args]);
<a name="line171"></a>  return this.$do(matchingExpectation, args);
<a name="line172"></a>};
<a name="line173"></a>
<a name="line174"></a>
<a name="line175"></a>/** @override */
<a name="line176"></a>goog.testing.LooseMock.prototype.$reset = function() {
<a name="line177"></a>  goog.testing.LooseMock.superClass_.$reset.call(this);
<a name="line178"></a>
<a name="line179"></a>  this.$expectations_ = new goog.structs.Map();
<a name="line180"></a>  this.$calls_ = [];
<a name="line181"></a>};
<a name="line182"></a>
<a name="line183"></a>
<a name="line184"></a>/** @override */
<a name="line185"></a>goog.testing.LooseMock.prototype.$replay = function() {
<a name="line186"></a>  goog.testing.LooseMock.superClass_.$replay.call(this);
<a name="line187"></a>
<a name="line188"></a>  // Verify that there are no expectations that can never be reached.
<a name="line189"></a>  // This can&#39;t catch every situation, but it is a decent sanity check
<a name="line190"></a>  // and it&#39;s similar to the behavior of EasyMock in java.
<a name="line191"></a>  var collections = this.$expectations_.getValues();
<a name="line192"></a>  for (var i = 0; i &lt; collections.length; i++) {
<a name="line193"></a>    var expectations = collections[i].getExpectations();
<a name="line194"></a>    for (var j = 0; j &lt; expectations.length; j++) {
<a name="line195"></a>      var expectation = expectations[j];
<a name="line196"></a>      // If this expectation can be called infinite times, then
<a name="line197"></a>      // check if any subsequent expectation has the exact same
<a name="line198"></a>      // argument list.
<a name="line199"></a>      if (!isFinite(expectation.maxCalls)) {
<a name="line200"></a>        for (var k = j + 1; k &lt; expectations.length; k++) {
<a name="line201"></a>          var laterExpectation = expectations[k];
<a name="line202"></a>          if (laterExpectation.minCalls &gt; 0 &amp;&amp;
<a name="line203"></a>              goog.array.equals(expectation.argumentList,
<a name="line204"></a>                  laterExpectation.argumentList)) {
<a name="line205"></a>            var name = expectation.name;
<a name="line206"></a>            var argsString = this.$argumentsAsString(expectation.argumentList);
<a name="line207"></a>            this.$throwException([
<a name="line208"></a>                &#39;Expected call to &#39;, name, &#39; with arguments &#39;, argsString,
<a name="line209"></a>                &#39; has an infinite max number of calls; can\&#39;t expect an&#39;,
<a name="line210"></a>                &#39; identical call later with a positive min number of calls&#39;
<a name="line211"></a>            ].join(&#39;&#39;));
<a name="line212"></a>          }
<a name="line213"></a>        }
<a name="line214"></a>      }
<a name="line215"></a>    }
<a name="line216"></a>  }
<a name="line217"></a>};
<a name="line218"></a>
<a name="line219"></a>
<a name="line220"></a>/** @override */
<a name="line221"></a>goog.testing.LooseMock.prototype.$verify = function() {
<a name="line222"></a>  goog.testing.LooseMock.superClass_.$verify.call(this);
<a name="line223"></a>  var collections = this.$expectations_.getValues();
<a name="line224"></a>
<a name="line225"></a>  for (var i = 0; i &lt; collections.length; i++) {
<a name="line226"></a>    var expectations = collections[i].getExpectations();
<a name="line227"></a>    for (var j = 0; j &lt; expectations.length; j++) {
<a name="line228"></a>      var expectation = expectations[j];
<a name="line229"></a>      if (expectation.actualCalls &gt; expectation.maxCalls) {
<a name="line230"></a>        this.$throwException(&#39;Too many calls to &#39; + expectation.name +
<a name="line231"></a>            &#39;\nExpected: &#39; + expectation.maxCalls + &#39; but was: &#39; +
<a name="line232"></a>            expectation.actualCalls);
<a name="line233"></a>      } else if (expectation.actualCalls &lt; expectation.minCalls) {
<a name="line234"></a>        this.$throwException(&#39;Not enough calls to &#39; + expectation.name +
<a name="line235"></a>            &#39;\nExpected: &#39; + expectation.minCalls + &#39; but was: &#39; +
<a name="line236"></a>            expectation.actualCalls);
<a name="line237"></a>      }
<a name="line238"></a>    }
<a name="line239"></a>  }
<a name="line240"></a>};
</pre>


</body>
</html>
